﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Cci;
using SpCop.Core.Model;
using SpCop.Model;

namespace SpCop.General.Receivers
{
    class FeatureReceiversExists : RuleBase
    {
        private static readonly Common.Logging.ILog Log = Common.Logging.LogManager.GetLogger(typeof(FeatureReceiversExists));
       
        
        private IList<Problem> _problems;
        private Package _package;

        public override IList<Problem> Check(Package package)
        {
            _package = package;
           _problems = base.Check(package);

            VisitCollection(package.Features,Visit);

           
            return _problems;
        }
        public override void Visit(Feature item)
        {
            if(!string.IsNullOrEmpty(item.ReceiverTypeName))
            {
               // _problems.Add(new Problem(GetResolution("ReceiverNotFound",item.ReceiverTypeName, item.Title)));
                try
                {
                    
                    try
                    {

                        
                        var typeNode = GetReceiverTypeNode(item);
                        
                        //var tt =   item.Package.Assemblies.SelectMany(a=>a.Types).SingleOrDefault(a=>a.FullName == typeNode.ToString() );

                        //if(tt != null)
                        //foreach (var member in tt.Members)
                        //{
                        //    if (!member.OverridesBaseClassMember || !member.Name.Name.StartsWith("Feature")) continue;
                            
                        //    var method = member as Method;
                        //    if (method != null)
                        //    {
                                    
                        //        new FxVisitor().VisitMethod(method);
                        //    }
                        //}
                        
                        typeNode.Dispatch(new CustomVisitor());

                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine("Failed to get feature receiver ! {0}", exception.Message);
                        
                        AddProblem("UnableToResolve", exception.Message);

                    }
                }catch(ArgumentException argexception)
                {

                }
            }

           

            base.Visit(item);
        }

    }
   
    internal class CustomVisitor : BaseMetadataVisitor
    {
        public override void Visit(IUnit unit)
        {
            base.Visit(unit);
        }

        public override void Visit(INamespaceTypeDefinition namespaceTypeDefinition)
        {
            foreach (var test in namespaceTypeDefinition.Members)
            {
                 Visit(test);
            }
           
           base.Visit(namespaceTypeDefinition);
        }

        public override void Visit(IMethodDefinition method)
        {
            method.Body.Dispatch(this);

            base.Visit(method);
        }

        public override void Visit(ITypeDefinitionMember typeMember)
        {
            
            base.Visit(typeMember);
        }
        public override void Visit(IMethodReference methodReference)
        {
            base.Visit(methodReference);
        }
        public override void Visit(IMethodImplementation methodImplementation)
        {
            base.Visit(methodImplementation);
        }
        public override void Visit(IMethodBody methodBody)
        {

            foreach (var operation in methodBody.Operations)
            {
               
            }


            base.Visit(methodBody);
        }
    }
}
